package com.zking.frame.TJ;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.zking.frame.TJ.JDBC.baseDao;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class register extends JFrame {
	private static final long serialVersionUID=1161213L;
	
	private JPanel pl_rg_main;
	
	private JLabel lb_logImg;//淘票票log
	private JLabel lb_title;//淘票票文字
	
	private JButton bt_bod;//分界线
	
	private JLabel lb_registerTitle;//标题
	
	//第一行，用户名
	private JLabel lb_rg_userName;//用户名
	private JTextField tf_rg_userName;//用户名输入框
	private boolean tf_rg_userNameFlag;//判断用户名输入是否规范
	
	//第二行，密码
	private JLabel lb_rg_password;//密码
	private JPasswordField pf_rg_password;//密码输入框
	private boolean tf_rg_passwordFlag;//判断密码输入是否规范
	
	private JLabel lb_rg_checkPassword;//验证密码
	private JPasswordField pf_rg_checkPassword;//密码输入框
	private boolean tf_rg_checkPasswordFlag;//判断密码输入是否规范
	
	private JLabel lb_rg_phone;//绑定手机
	private JTextField tf_rg_phone;//手机号输入框
	private boolean tf_rg_phoneFlag;//判断手机号输入是否规范
	
	private JButton bt_rg_return;//返回登录
	private JButton bt_register;//注册
	
	public register() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		pl_rg_main=new JPanel();
		pl_rg_main.setLayout(null);
		pl_rg_main.setBackground(Color.WHITE);
		c.add(pl_rg_main);
		
		lb_logImg = new JLabel(new ImageIcon("log.jpg"));
		lb_logImg.setBounds(196, 84, 75, 75);
		pl_rg_main.add(lb_logImg);
		
		//淘票票文字
		lb_title = new JLabel("淘票票");
		lb_title.setFont(new Font("宋体", Font.PLAIN, 40));
		lb_title.setBounds(296, 84, 125, 75);
		lb_title.setForeground(Color.RED);
		pl_rg_main.add(lb_title);
		
		//分界线
		bt_bod = new JButton();
		bt_bod.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		bt_bod.setBounds(0,172, 1500, 5);
		bt_bod.setBackground(new Color(224,32,37));
		bt_bod.setEnabled(false);
		pl_rg_main.add(bt_bod);
		
		//登录标题
		lb_registerTitle = new JLabel("用户注册");
		lb_registerTitle.setBounds(401, 208, 87, 42);
		lb_registerTitle.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		pl_rg_main.add(lb_registerTitle);
		
		//用户名
		lb_rg_userName = new JLabel("用户名");
		lb_rg_userName.setBounds(401, 263, 60, 42);
		lb_rg_userName.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		pl_rg_main.add(lb_rg_userName);
		
		//用户名输入框
		tf_rg_userName = new JTextField();
		tf_rg_userName.setBounds(499, 263, 287, 42);
		tf_rg_userName.addFocusListener(new FocusListener() {
			
			@Override
			public void focusGained(FocusEvent e) {
				tf_rg_userName.setForeground(Color.BLACK);
				if(tf_rg_userNameFlag==false) {
					tf_rg_userName.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				tf_rg_userNameFlag=Pattern.matches("^(?!.*\\s).{1,8}$", tf_rg_userName.getText());
				if(tf_rg_userName.getText().isEmpty()) {
					tf_rg_userName.setText("用户名不能为空");
					tf_rg_userNameFlag=false;
					tf_rg_userName.setForeground(Color.RED);
				}else if(tf_rg_userNameFlag==false) {
					tf_rg_userName.setText("用户名为长度为1-8个字符，请重新输入");
					tf_rg_userName.setForeground(Color.RED);
				}
			}
		});
		pl_rg_main.add(tf_rg_userName);
		
		//密码
		lb_rg_password = new JLabel("密码");
		lb_rg_password.setBounds(401, 318, 60, 42);
		lb_rg_password.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		pl_rg_main.add(lb_rg_password);
		
		//密码输入框
		pf_rg_password = new JPasswordField();
		pf_rg_password.setBounds(499, 318, 287, 42);
		pf_rg_password.addFocusListener(new FocusListener() {
			
			@Override
			public void focusGained(FocusEvent e) {
				pf_rg_password.setForeground(Color.BLACK);
				if(tf_rg_passwordFlag==false) {
					pf_rg_password.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				tf_rg_passwordFlag=Pattern.matches("^(?!.*\\s).{6,16}$", pf_rg_password.getText());
				if(pf_rg_password.getText().isEmpty()) {
					pf_rg_password.setText("密码不能为空");
					tf_rg_passwordFlag=false;
					pf_rg_password.setForeground(Color.RED);
				}else if(tf_rg_passwordFlag==false) {
					pf_rg_password.setText("密码为长度为6-16个字符且区分大小写，请重新输入");
					pf_rg_password.setForeground(Color.RED);
				}
			}
		});
		pl_rg_main.add(pf_rg_password);
		
		//验证密码
		lb_rg_checkPassword = new JLabel("确认密码");
		lb_rg_checkPassword.setBounds(401, 373, 87, 42);
		lb_rg_checkPassword.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		pl_rg_main.add(lb_rg_checkPassword);
		
		//验证密码输入框
		pf_rg_checkPassword = new JPasswordField();
		pf_rg_checkPassword.setBounds(499, 373, 287, 42);
		pf_rg_checkPassword.addFocusListener(new FocusListener() {
			
			@Override
			public void focusGained(FocusEvent e) {
				pf_rg_checkPassword.setForeground(Color.BLACK);
				if(tf_rg_checkPasswordFlag==false) {
					pf_rg_checkPassword.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				tf_rg_checkPasswordFlag=pf_rg_checkPassword.getText().equals(pf_rg_password.getText());//确认密码
				if(pf_rg_checkPassword.getText().isEmpty()) {
					tf_rg_checkPasswordFlag=false;
					pf_rg_checkPassword.setText("密码不能为空");
					pf_rg_checkPassword.setForeground(Color.RED);
				}else if(tf_rg_checkPasswordFlag==false) {
					pf_rg_checkPassword.setText("两次输入的密码不一致");
					pf_rg_checkPassword.setForeground(Color.RED);
				}
			}
		});
		pl_rg_main.add(pf_rg_checkPassword);
		
		//绑定手机
		lb_rg_phone = new JLabel("绑定手机");
		lb_rg_phone.setBounds(401, 428, 87, 42);
		lb_rg_phone.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		
		pl_rg_main.add(lb_rg_phone);
		
		//手机号输入框
		tf_rg_phone = new JTextField();
		tf_rg_phone.setBounds(499, 431, 287, 42);
		tf_rg_phone.addFocusListener(new FocusListener() {
			
			@Override
			public void focusGained(FocusEvent e) {
				tf_rg_phone.setForeground(Color.BLACK);
				if(tf_rg_phoneFlag==false) {
					tf_rg_phone.setText("");
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
				tf_rg_phoneFlag=Pattern.matches("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$", tf_rg_phone.getText());
				if(tf_rg_phone.getText().isEmpty()) {
					tf_rg_phone.setText("手机号不能为空");
					tf_rg_phone.setForeground(Color.RED);
				}else if(tf_rg_phoneFlag==false) {
					tf_rg_phone.setText("手机号不正确");
					tf_rg_phone.setForeground(Color.RED);
				}
			}
		});
		pl_rg_main.add(tf_rg_phone);
		
		//返回登录页面按钮
		bt_rg_return = new JButton("返回登录");
		bt_rg_return.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		bt_rg_return.setBounds(469, 497, 107, 40);
		bt_rg_return.setFocusPainted(false);
		bt_rg_return.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseClicked(MouseEvent e){
				new login();
				dispose();
			}
			
			@Override
			public void mouseEntered(MouseEvent e){//鼠标进入  
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
			}
			
			@Override
			public  void mouseExited(MouseEvent e){//鼠标移除  
				setCursor(Cursor.getDefaultCursor());
			}
			
		});
		pl_rg_main.add(bt_rg_return);
		
		
		//立即注册按钮
		bt_register = new JButton("注册");
		bt_register.setBounds(646, 497, 107, 40);
		bt_register.setFocusPainted(false);
		bt_register.addMouseListener(new MouseAdapter(){
			
			@Override
			public void mouseClicked(MouseEvent e){
				String sql =  "INSERT INTO USER(username,password,phone,balance,headPic) VALUES('"+tf_rg_userName.getText()+"','"+pf_rg_password.getText()+"','"+tf_rg_phone.getText()+"',"+0+",'"+"D:\\\\\\\\项目演示头像\\\\\\\\headPortrait.png"+"')";
				boolean nameFlag=false;
				boolean phoneFlag=false;
				List <String> names = baseDao.findAllUsername();
				List <String> phones = baseDao.findAllPhone();
				if(names.contains(tf_rg_userName.getText())) {
					nameFlag=false;
				}else {
					nameFlag=true;;
				}
				if(phones.contains(tf_rg_phone.getText())) {
					phoneFlag=false;
				}else {
					phoneFlag=true;
				}
				if(tf_rg_userNameFlag==false||tf_rg_passwordFlag==false||tf_rg_checkPasswordFlag==false||tf_rg_phoneFlag==false) {
					JOptionPane.showMessageDialog(new JPanel(), "输入信息有误","警告",JOptionPane.WARNING_MESSAGE);
				}else if(nameFlag==false){
					JOptionPane.showMessageDialog(new JPanel(), "用户名已被占用","警告",JOptionPane.WARNING_MESSAGE);
				}else if(phoneFlag==false) {
					JOptionPane.showMessageDialog(new JPanel(), "手机号已被占用","警告",JOptionPane.WARNING_MESSAGE);
				} else{
					baseDao.addUser(sql);
					System.out.println(sql);
					JOptionPane.showMessageDialog(null, "注册成功！");
					new login();
					dispose();
				}
			}
		
			@Override
			public void mouseEntered(MouseEvent e){//鼠标进入  
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  
			} 
			
			@Override
			public  void mouseExited(MouseEvent e){//鼠标移除  
				setCursor(Cursor.getDefaultCursor());
			}
		
		});
		pl_rg_main.add(bt_register);
		
		setTitle("淘票票网");//标题
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//点击关闭按钮退出
		setSize(1500, 1000);//尺寸
		setLocationRelativeTo(null);//居中
		setVisible(true);//可见
	}
	
	public static void main(String[] args) {
		new register();
	}
}
